/* eslint-disable no-unsafe-optional-chaining */
/* eslint-disable @typescript-eslint/no-use-before-define */
/* eslint-disable @typescript-eslint/no-unused-vars */
import { DeleteOutlined, ExclamationCircleFilled, ExportOutlined, PlusOutlined } from "@ant-design/icons";

import CustomerTable from "@/components/CustomerTable";
import { SyncOutlined } from "@ant-design/icons";
import { ActionType, PageContainer, ProColumns } from "@ant-design/pro-components";
import type { FormInstance, MenuProps } from "antd";
import { Button, Divider, Dropdown, Input, Modal, Popconfirm, Space, Typography, message } from "antd";
import React, { useEffect, useRef, useState } from "react";
import DictTypeForm, { DictTypeParams } from "./components/dictTypeForm";
import { getDict } from "@/services/api/module/system/dict";
import { getPageList, remove, save, update } from "@/services/api/module/tenant/developer/dictType";
import { syncDict } from "@/services/api/module/tenant/developer/dict";
import FilterDrawer from "@/components/FilterDrawer";
import { dictTabs, createTableColumns, pagination } from "./constants";
import { useRequest } from "ahooks";
import { Panel, PanelGroup, PanelResizeHandle } from "react-resizable-panels";
import DictDataTableList from "./components/dictDataTableList";
import { useTableScroll } from "@/hooks/useTableScroll";
import { dict } from "@/utils/DictionaryCacheManager";
import DictTypeList from "./components/DictTypeList";
const DictTableList: React.FC = () => {
	const [open, setOpen] = useState<boolean>(false);
	const [dictTypeOpen, setDictTypeOpen] = useState<boolean>(false);
	const [currentDictType, setCurrentDictType] = useState<string>("");
	const [currentDictName, setCurrentDictName] = useState<string>("");
	const [currentRowId, setCurrentRowId] = useState<undefined | number>(undefined);
	const actionRef = useRef<ActionType>();
	const [dictCategory, setDictCapteGory] = useState<any>();
	const [statusOptions, setStatusOptions] = useState<any>();

	// 初始化字典
	useEffect(() => {
		dict.getDict("sys_normal_disable").then(data => {
			setStatusOptions(data);
		});
		dict.getDict("sys_category_type").then(data => {
			setDictCapteGory(data);
		});
	}, []);
	const handleOpenData = (title?: string) => {
		setOpen(!open);
	};
	// 删除
	const handleRemove = async (ids: number | Array<number>) => {
		return new Promise((reslove, reject) => {
			remove(ids)
				.then((res: any) => {
					message.success(res.msg);
					if (actionRef.current) {
						actionRef.current.reload();
					}
					reslove(true);
				})
				.catch(() => {
					reject(true);
				});
		});
	};
	// 保存
	const handleSave = async (values: DictTypeParams) => {
		return new Promise((resolve, reject) => {
			save({ ...values } as DictTypeParams)
				.then((res: any) => {
					message.success(res.msg);
					handleTableForm();
					resolve(true);
				})
				.catch(() => {
					reject(false);
				});
		});
	};
	// 修改
	const handleUpdate = async (values: DictTypeParams) => {
		return new Promise((resolve, reject) => {
			update({ ...values } as DictTypeParams)
				.then((res: any) => {
					message.success(res.msg);
					handleTableForm();
					resolve(true);
				})
				.catch(() => {
					reject(false);
				});
		});
	};
	// 关闭表单刷新列表
	const handleTableForm = async () => {
		setDictTypeOpen(false);
		setCurrentRowId(undefined);
		actionRef?.current?.reload();
	};
	// 关闭弹出表单
	const handleCloseForm = async () => {
		setCurrentRowId(undefined);
		setDictTypeOpen(false);
	};
	// // 刷新缓存
	// const syncCatchDict = async (): Promise<any> => {
	// 	run();
	// };
	// // 刷新缓存
	// const { loading, run } = useRequest(handleAsync, {
	// 	throttleWait: 3000,
	// 	manual: true
	// });
	// // 刷新缓存
	// const handleAsync = () => {
	// 	return new Promise((reslove, reject) => {
	// 		syncDict()
	// 			.then(res => {
	// 				message.success(res.msg);
	// 				reslove(res);
	// 			})
	// 			.catch(() => {
	// 				reject("");
	// 			});
	// 	});
	// };

	const handleSelect = (record: any) => {
		setCurrentDictType(record?.dictType);
		setCurrentDictName(record?.dictName);
	};
	return (
		<>
			<PanelGroup direction="horizontal">
				<Panel defaultSize={30} minSize={25} maxSize={30}>
					<DictTypeList onSelect={handleSelect} />
				</Panel>
				<PanelResizeHandle style={{ width: 5 }} />
				<Panel minSize={50}>
					<DictDataTableList dictType={currentDictType} dictName={currentDictName} statusOptions={statusOptions} />
				</Panel>
			</PanelGroup>
			<DictTypeForm
				open={dictTypeOpen}
				onCancel={handleCloseForm}
				dictCategory={dictCategory}
				onSubmit={async (values: DictTypeParams) => {
					return values.id ? await handleUpdate(values) : await handleSave(values);
				}}
				id={currentRowId}
			/>
		</>
	);
};

export default DictTableList;
